﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Windows.Forms;
using VietKinsNutrition.Data;
using DevExpress.XtraGrid.Views.Grid;

namespace VietKinsNutrition.Bussiness
{
    public class ClsTblDm_XuatkhoBLL
    {
        // thomnd
        ClsTblDm_XuatkhoDAL ClsTblDm_Xuatkho_dal = new ClsTblDm_XuatkhoDAL();
        ClsTblDm_NamhocBLL ClsTblDm_Namhocbll = new ClsTblDm_NamhocBLL();
        public void Bind_GridDanhsachnguyenlieudukienmua(DataGridView GridDanhsachnguyenlieudukienmua, DateTime values)
        {
            GridDanhsachnguyenlieudukienmua.DataSource = Table_GridDanhsachnguyenlieudukienmua(values);
        }
        public void Insert_XuathangchokhoiBy(DateTime NgayNX, int KhoilopId, int Thucpham_thucanId, string Soluong, string Gia)
        {
            ClsTblDm_Xuatkho_dal.Insert_XuathangchokhoiBy(NgayNX, KhoilopId, Thucpham_thucanId, Soluong, Gia);
        }
        public void Delete_XuathangchokhoiBy(DateTime NgayNX, int KhoilopId, int Thucpham_thucanId)
        {
            ClsTblDm_Xuatkho_dal.Delete_XuathangchokhoiBy(NgayNX, KhoilopId, Thucpham_thucanId);
        }
        public void Delete_ALL_XuathangchokhoiBy(DateTime NgayNX, int KhoilopId)
        {
            ClsTblDm_Xuatkho_dal.Delete_ALL_XuathangchokhoiBy(NgayNX, KhoilopId);
        }
        public void Delete_XuathangbyNgay(int Mahoadon)
        {
            ClsTblDm_Xuatkho_dal.Delete_XuathangbyMahoadon(Mahoadon);
        }
        public void DeleteHoadon_XuathangbyNgay(DateTime NgayNX)
        {
            ClsTblDm_Xuatkho_dal.DeleteHoadon_XuathangbyNgay(NgayNX);
        }
        public void Delete_All_Hoadon_XuathangAndThucphamxuatbyHoadonId(int HoadonxuatId)
        {
            ClsTblDm_Xuatkho_dal.Delete_All_Hoadon_XuathangAndThucphamxuatbyHoadonId(HoadonxuatId);
        }
        public DataTable GetTable_TblXuathanghoa_GetbyNgayNX(DateTime values)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.GetTable_TblXuathanghoa_GetbyNgayNX(values);
            Tbl.Columns.Remove("Ngay");
            return Tbl;
        }
        public DataTable TblHoadonnhap_GetId_ByLonhonNgay(DateTime NgayNX)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.TblHoadonnhap_GetId_ByLonhonNgay(NgayNX);
            return Tbl;
        }
        public DataTable TblHoadonnhapxuat_GetId_ByLonhonNgay(DateTime NgayNX)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.TblHoadonnhapxuat_GetId_ByLonhonNgay(NgayNX);
            return Tbl;
        }
        public void SetHoadonbyBgDatetimeAndEndDatetime(GridView Dg, DateTime BgDate, DateTime EndDate)
        {
            DataSet Tbl = new DataSet();
            Tbl = ClsTblDm_Xuatkho_dal.SetHoadonbyBgDatetimeAndEndDatetime(BgDate, EndDate);
            Dg.GridControl.DataSource = Tbl.Tables[0];
        }

        public DataTable TblHoadonnhapxuatReturnDSxuatthucpham_PLG_ByNgay(DateTime NgayNX)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.TblHoadonnhapxuatReturnDSxuatthucpham_PLG_ByNgay(NgayNX);
            return Tbl;
        }
        public DataTable NewTblHoadonnhapxuatReturnDSxuatthucpham_PLG_ByNgay(DateTime NgayNX, DataTable TblDm_Khoilop)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.NewTblHoadonnhapxuatReturnDSxuatthucpham_PLG_ByNgay(NgayNX, TblDm_Khoilop);
            return Tbl;
        }
        public DataTable TblXuatchokhoilop_ByKhoilop_Ngay(DateTime NgayNX, int KhoilopId)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.TblXuatchokhoilop_ByKhoilop_Ngay(NgayNX, KhoilopId);
            return Tbl;
        }
        public DataTable TblXuatchokhoilop_Ngay(DateTime NgayNX)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.TblXuatchokhoilop_Ngay(NgayNX);
            return Tbl;

        }
        public void UpdatePhatsinhByNgay(string Phatsinh, DateTime NgayNX)
        {
            ClsTblDm_Xuatkho_dal.UpdatePhatsinhByNgay(Phatsinh, NgayNX);
        }
        public DataTable TblHoadonnhap_GetId_ByNgay(DateTime NgayNX)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.TblHoadonnhap_GetId_ByNgay(NgayNX);
            return Tbl;
        }
        public DataTable TblHoadonnhapxuat_GetId_ByNgay(DateTime NgayNX)
        {
            DataTable Tbl = new DataTable();
            Tbl = ClsTblDm_Xuatkho_dal.TblHoadonnhapxuat_GetId_ByNgay(NgayNX);
            return Tbl;
        }
        public DataTable Table_GridDanhsachnguyenlieudukienmua(DateTime values)
        {
            DataTable Tbl;
            Tbl = this.ClsTblDm_Xuatkho_dal.GetTable_GridDanhsachnguyenlieudukienmua(values);
            DataTable ReturnTbl = new DataTable();
            ReturnTbl = Tbl.Clone();
            foreach (DataRow Row in Tbl.Rows)
            {
                ReturnTbl.Rows.Add(Row["Thucpham_thucanId"],
                    Row["DonvitinhId"], Row["Tenthucan"],
                    Row["Tendonvitinh"], Row["Soluongcon"],
                    Row["Soluong"], Row["Dongia"],
                    Row["Thanhtien"], Row["Ngay"]);

            }
            return ReturnTbl;
        }
        public void Bind_GridDanhsachbaoan(DataGridView GridDanhsachbaoan, DateTime Ngay, int NamhocID)
        {
            this.ClsTblDm_Xuatkho_dal.GetTable_GridDanhsachbaoan(Ngay, NamhocID).Columns.Remove("Ngay");
            GridDanhsachbaoan.DataSource = this.ClsTblDm_Xuatkho_dal.GetTable_GridDanhsachbaoan(Ngay, NamhocID);
        }
        public DataTable GetTbl_TblNguyenlieutonkho()
        {
            return this.ClsTblDm_Xuatkho_dal.GetTbl_TblNguyenlieutonkho();
        }
        public DataTable Table_DanhsachnguyenlieudukienvaTonkho(DateTime values)
        {
            DataTable TblDukien = this.Table_GridDanhsachnguyenlieudukienmua(values);
            DataTable TblTonkho = this.GetTbl_TblNguyenlieutonkho();
            DataTable ReturnTbl = new DataTable();
            ReturnTbl = TblDukien.Clone();
            foreach (DataRow Rw in TblDukien.Rows)
            {
                ReturnTbl.Rows.Add(Rw["Thucpham_thucanId"],
                     Rw["DonvitinhId"], Rw["Tenthucan"],
                    Rw["Tendonvitinh"], Rw["Soluongcon"], Rw["Soluong"],
                    0, Rw["Thanhtien"], Rw["Ngay"]);
            }
            foreach (DataRow Rwreturn in ReturnTbl.Rows)
            {
                foreach (DataRow RwTonkho in TblTonkho.Rows)
                {
                    if (RwTonkho["Thucpham_thucanId"].ToString().Trim() == Rwreturn["Thucpham_thucanId"].ToString().Trim())
                    {
                        Rwreturn["Soluongcon"] = RwTonkho["Soluong"];
                        TblTonkho.Rows.Remove(RwTonkho);
                        break;
                    }
                }
            }
            //  ReturnTbl.Columns.Remove("Ngay");
            return ReturnTbl;
        }
        string DongiaDK(DataTable Tbl, int Id)
        {
            string sValue = "0";
            foreach (DataRow Rw in Tbl.Rows)
            {
                if (Id == int.Parse(Rw["thucpham_thucanId"].ToString()))
                {
                    sValue = Rw["Dongia"].ToString();
                    return sValue;
                }
            }
            return sValue;
        }
        public DataTable Table_NhapdanhsachnguyenlieudukienvaTonkho(DateTime values, DataGridView GrXuat)
        {
            DataTable TblDukien = this.Table_GridDanhsachnguyenlieudukienmua(values);
            //DataTable TblTonkho = this.GetTbl_TblNguyenlieutonkho();
            DataTable ReturnTbl = new DataTable();
            ReturnTbl = TblDukien.Clone();
            foreach (DataGridViewRow Rw in GrXuat.Rows)
            {
                string sNhap = "";
                if ((float.Parse(Rw.Cells["X_Soluongxuat"].Value.ToString()) - float.Parse(Rw.Cells["X_Soluongcon"].Value.ToString())) > 0)
                {
                    sNhap = (float.Parse(Rw.Cells["X_Soluongxuat"].Value.ToString()) - float.Parse(Rw.Cells["X_Soluongcon"].Value.ToString())).ToString();

                    if (float.Parse(Rw.Cells["X_Dongia"].Value.ToString()) > 0)
                    {
                        string TT = ((float.Parse(Rw.Cells["X_Soluongxuat"].Value.ToString()) - float.Parse(Rw.Cells["X_Soluongcon"].Value.ToString())) * float.Parse(Rw.Cells["X_Dongia"].Value.ToString())).ToString();
                        ReturnTbl.Rows.Add(Rw.Cells["X_Thucpham_ThucanId"].Value,
                                 Rw.Cells["X_DonvitinhId"].Value, Rw.Cells["X_Tenthucan"].Value,
                                Rw.Cells["X_Tendonvitinh"].Value, Rw.Cells["X_Soluongcon"].Value, sNhap,
                              Rw.Cells["X_Dongia"].Value.ToString(), TT, values);
                    }
                    else
                    {
                        string Dga = DongiaDK(TblDukien, int.Parse(Rw.Cells["X_Thucpham_ThucanId"].Value.ToString()));
                        string TT = (int.Parse(Dga) * (float.Parse(Rw.Cells["X_Soluongxuat"].Value.ToString()) - float.Parse(Rw.Cells["X_Soluongcon"].Value.ToString()))).ToString();
                        ReturnTbl.Rows.Add(Rw.Cells["X_Thucpham_ThucanId"].Value,
                                Rw.Cells["X_DonvitinhId"].Value, Rw.Cells["X_Tenthucan"].Value,
                               Rw.Cells["X_Tendonvitinh"].Value, Rw.Cells["X_Soluongcon"].Value, sNhap,
                              Dga, TT, values);
                    }
                }
            }
            return ReturnTbl;
        }
        public DataTable Table_NhapdanhsachnguyenlieudukienvaTonkho(DateTime values, DataTable GrXuat)
        {
            DataTable TblDukien = this.Table_GridDanhsachnguyenlieudukienmua(values);
            DataTable ReturnTbl = new DataTable();
            ReturnTbl = TblDukien.Clone();
            foreach (DataRow Rw in GrXuat.Rows)
            {
                string sNhap = "";
                if ((float.Parse(Rw["Soluongxuat"].ToString()) - float.Parse(Rw["Soluongton"].ToString())) > 0)
                {
                    sNhap = (float.Parse(Rw["Soluongxuat"].ToString()) - float.Parse(Rw["Soluongton"].ToString())).ToString();

                    if (float.Parse(Rw["Dongia"].ToString()) > 0)
                    {
                        string TT = ((float.Parse(Rw["Soluongxuat"].ToString()) - float.Parse(Rw["Soluongton"].ToString())) * float.Parse(Rw["Dongia"].ToString())).ToString();
                        ReturnTbl.Rows.Add(Rw["Thucpham_ThucanId"].ToString(),
                                 Rw["DonvitinhId"].ToString(), Rw["Tenthucan"].ToString(),
                                Rw["Tendonvitinh"].ToString(), Rw["Soluongton"].ToString(), sNhap,
                              Rw["Dongia"].ToString(), TT, values);
                    }
                    else
                    {
                        string Dga = DongiaDK(TblDukien, int.Parse(Rw["Thucpham_ThucanId"].ToString()));
                        string TT = (int.Parse(Dga) * (float.Parse(Rw["Soluongxuat"].ToString()) - float.Parse(Rw["Soluongton"].ToString()))).ToString();
                        ReturnTbl.Rows.Add(Rw["Thucpham_ThucanId"].ToString(),
                                Rw["DonvitinhId"].ToString(), Rw["Tenthucan"].ToString(),
                               Rw["Tendonvitinh"].ToString(), Rw["Soluongton"].ToString(), sNhap,
                              Dga, TT, values);
                    }
                }
            }
            return ReturnTbl;
        }
        public DataTable Table_Xuatkhoavachengia(DateTime values)
        {
            DataTable Tbl = new DataTable();
            Tbl = Table_DanhsachnguyenlieudukienvaTonkho(values);
            ClsTblDm_NhapthucphamBLL ClsTblDm_Nhapthucphambll = new ClsTblDm_NhapthucphamBLL();
            DataTable TblGia = new DataTable();
            foreach (DataRow Rw in Tbl.Rows)
            {
                TblGia = ClsTblDm_Nhapthucphambll.GianhapByNgayNXandThucphamthucanId(values, int.Parse(Rw["Thucpham_thucanId"].ToString()));
                if (TblGia.Rows.Count > 0)
                {
                    Rw["Dongia"] = TblGia.Rows[0]["Dongia"];
                    Rw["Thanhtien"] = float.Parse(Rw["Dongia"].ToString()) * float.Parse(Rw["Soluong"].ToString());
                }
            }
            return Tbl;
        }

        public DataTable TblThucphamnhapById(int IdThucpham_thucan)
        {
            DataTable ReturnTbl = new DataTable();
            ReturnTbl = ClsTblDm_Xuatkho_dal.TblThucphamnhapById(IdThucpham_thucan);
            return ReturnTbl;
        }
        public DataTable GetTbl_TblNguyenlieutonkhovagia(int Thucpham_ThucanId)
        {
            DataTable ReturnTbl = new DataTable();
            DataTable dt;
            dt = this.ClsTblDm_Xuatkho_dal.GetTbl_TblNguyenlieutonkhovagia(Thucpham_ThucanId);
            ReturnTbl = dt.Clone();

            ReturnTbl.Columns.Add("Soluonglayxuat", ReturnTbl.Columns["Soluong"].DataType);
            foreach (DataRow Rw in dt.Rows)
            {
                if (System.Math.Round(float.Parse(Rw["Soluong"].ToString()), 3).ToString().Trim() != "0")
                {
                    ReturnTbl.Rows.Add(Rw["Soluong"], Rw["Thucpham_thucanId"], Rw["Dongia"], 0);
                }
            }
            return ReturnTbl;
        }
        public void TblHoadonnhapxuat_Add(DateTime NgayNX, float Tongtien)
        {
            ClsTblDm_Xuatkho_dal.TblHoadonnhapxuat_Add(NgayNX, Tongtien);
        }

        public void TblHoadonnhapxuat_DeleByNamhocId(int NamhocId)
        {
            ClsTblDm_Xuatkho_dal.TblHoadonnhapxuat_DeleByNamhocId(NamhocId);
        }

        public void TblHoadonnhapxuat_DeleByHoadon(int HoadonnhapxuatId)
        {
            ClsTblDm_Xuatkho_dal.TblHoadonnhapxuat_DeleByHoadon(HoadonnhapxuatId);
        }

        public void TblXuathanghoagiatrungbinh_DeleByHoadon(int HoadonnhapxuatId)
        {
            ClsTblDm_Xuatkho_dal.TblXuathanghoagiatrungbinh_DeleByHoadon(HoadonnhapxuatId);
        }
        public void TblNhapxuathanghoa_DeleteByhoadon(int HoadonnhapxuatId)
        {
            ClsTblDm_Xuatkho_dal.TblNhapxuathanghoa_DeleteByhoadon(HoadonnhapxuatId);
        }
        public DataTable TblHoadonnhapxuat_GetId_ByFindNgay(DateTime NgayNX)
        {
            return ClsTblDm_Xuatkho_dal.TblHoadonnhapxuat_GetId_ByNgay(NgayNX);
        }
        public void Edit_Hoadondonxuat(int HoadonnhapxuatId, float Tongtien)
        {
            ClsTblDm_Xuatkho_dal.Edit_Hoadondonxuat(HoadonnhapxuatId, Tongtien);
        }
        public void TblNhapxuathanghoa_Add(int HoadonnhapxuatId, int DonvitinhId, int Thucpham_ThucanId, string Soluong, string Dongia, string Soluongcon)
        {
            ClsTblDm_Xuatkho_dal.TblNhapxuathanghoa_Add(HoadonnhapxuatId, DonvitinhId, Thucpham_ThucanId, Soluong, Dongia, Soluongcon);
        }
        public DataTable GetDonvitinhIdById(int Thucpham_thucanId)
        {
            return ClsTblDm_Xuatkho_dal.GetDonvitinhIdById(Thucpham_thucanId);
        }
        public void TblXuathanghoagiatrungbinh_Add(int HoadonnhapxuatId, int DonvitinhId, int Thucpham_ThucanId, float Dongiatrungbinh, float Soluong, float Soluongcon, float Thanhtien)
        {
            ClsTblDm_Xuatkho_dal.TblXuathanghoagiatrungbinh_Add(HoadonnhapxuatId, DonvitinhId, Thucpham_ThucanId, Dongiatrungbinh, Soluong, Soluongcon, Thanhtien);
        }
        public int Return_iTongsosuatan(DateTime Ngay, int NamhocID)
        {
            int iReturn = 0;
            DataTable dt;
            dt = ClsTblDm_Xuatkho_dal.GetTable_GridDanhsachbaoan(Ngay, NamhocID);
            foreach (DataRow Row in dt.Rows)
            {
                iReturn = iReturn + int.Parse(Row["Tongsosuat"].ToString());
            }
            return iReturn;
        }
        public float Return_iTongtienandukien(DateTime Ngay)
        {
            float fReturn = 0;
            foreach (DataRow Row in this.Table_GridDanhsachnguyenlieudukienmua(Ngay).Rows)
            {
                fReturn = fReturn + int.Parse(Row["Thanhtien"].ToString());
            }
            return fReturn;
        }
        public void TblTemp_Add(int DonvitinhId, int Thucpham_ThucanId, float Soluonglayxuat, float Soluongcon, float Dongia)
        {
            ClsTblDm_Xuatkho_dal.TblTemp_Add(DonvitinhId, Thucpham_ThucanId, Soluonglayxuat, Soluongcon, Dongia);
        }
        public DataTable TblTemp_Get()
        {
            return ClsTblDm_Xuatkho_dal.TblTemp_Get();
        }
        public void TblTemp_Delete()
        {
            ClsTblDm_Xuatkho_dal.TblTemp_Delete();
        }
        public DataTable TblTemp_GetbyThucpham_thucanId(int Thucpham_thucanId)
        {
            return ClsTblDm_Xuatkho_dal.TblTemp_GetbyThucpham_thucanId(Thucpham_thucanId);
        }
        public DataTable TonghophoadondichoByNgay(DateTime NgayNX)
        {
            return this.ClsTblDm_Xuatkho_dal.TonghophoadondichoByNgay(NgayNX);
        }

        public void TblTemp_DeleteByThucpham_thucanId(int Thucpham_thucanId)
        {
            ClsTblDm_Xuatkho_dal.TblTemp_DeleteByThucpham_thucanId(Thucpham_thucanId);
        }

        public DataTable TblChitiethoadonnhapxuat_ByNgay(DateTime NgayNX, int Thucpham_thucanId)
        {
            return ClsTblDm_Xuatkho_dal.TblChitiethoadonnhapxuat_ByNgay(NgayNX, Thucpham_thucanId);
        }

        public void ExportFileExel(DataGridView tblNhanvienDataGridView, SaveFileDialog File, TextBox txt, int SHD)
        {
            string sTenso = null, sTentruong = null, sTenphong = null, sTieude = null;
            ClsTblTruonghocBLL ClsTblTruonghocbll = new ClsTblTruonghocBLL();
            DataTable Tbl = ClsTblTruonghocbll.ReturnTblTRuong();
            ClsExport2ExcelBLL ClsExport2Excelbll = new ClsExport2ExcelBLL();
            if (Tbl.Rows.Count > 0)
            {
                DataRow dr = Tbl.Rows[0];
                sTenso = dr["TensoGD"].ToString();
                sTentruong = dr["Tentruong"].ToString();
                sTenphong = dr["TenphongGD"].ToString();
                sTieude = " HÓA ĐƠN XUẤT HÀNG TRONG NGÀY ";
                File.FileName = sTieude;
            }
            ClsExport2Excelbll.ExportFileExel(SHD, sTieude, sTenso, sTenphong, sTentruong, File.FileName + ".xls", tblNhanvienDataGridView, txt);
        }
        public void ExportFileExel_byKhoilop(DataGridView tblNhanvienDataGridView, SaveFileDialog File, TextBox txt, int SHD, string Khoilop)
        {
            string sTenso = null, sTentruong = null, sTenphong = null, sTieude = null;
            ClsTblTruonghocBLL ClsTblTruonghocbll = new ClsTblTruonghocBLL();
            DataTable Tbl = ClsTblTruonghocbll.ReturnTblTRuong();
            ClsExport2ExcelBLL ClsExport2Excelbll = new ClsExport2ExcelBLL();
            if (Tbl.Rows.Count > 0)
            {
                DataRow dr = Tbl.Rows[0];
                sTenso = dr["TensoGD"].ToString();
                sTentruong = dr["Tentruong"].ToString();
                sTenphong = dr["TenphongGD"].ToString();
                sTieude = "THỰC PHẨM XUẤT CHO KHỐI " + Khoilop;
                File.FileName = sTieude;
            }
            ClsExport2Excelbll.ExportFileExel_Xuatchokhoilop(SHD, sTieude, sTenso, sTenphong, sTentruong, File.FileName + ".xls", tblNhanvienDataGridView, txt);
        }
    }
}
